package com.jqb.hibernatebbs.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.Query;
import java.util.List;

/**
 * 自定义Session工具类(方法是否要用静态取决与并发性)
 * */
final public class SessionHandler {

	private static SessionFactory sessionFactory = null;
	private static ThreadLocal<Session> threadLocal = null;

	/***
	 * 初始化静态变量
	 */
	static {
		sessionFactory = new Configuration().configure().buildSessionFactory();
		threadLocal = new ThreadLocal<Session>();
	}

	private SessionHandler() {
	}

	/***
	 * 获取全新session
	 * 
	 * @return
	 */
	public static Session getSession() {
		Session session = sessionFactory.openSession();
		return session;
	}

	/***
	 * 获取线程session
	 * 
	 * @return
	 */
	public static Session getCurrentSession() {
		Session session = threadLocal.get();
		if (session == null) {
			session = sessionFactory.openSession();
			threadLocal.set(session);
		}
		return session;
	}

	/***
	 * 获取查询结果
	 * 
	 * @param hql
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List HQLQuery(String hql, String[] params) {
		List list = null;
		Session session = null;
		try {
			session = getSession();
			Query query = session.createQuery(hql);
			// 过滤没参数的情况
			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					query.setString(i, params[i]);
				}
			}
			// 查询
			list = query.list();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return list;
	}

	/***
	 * 获取查询结果(带分页)
	 * 
	 * @param hql
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static List HQLPagingQuery(String hql, String[] params,
			int pageSize, int pageNum) {
		List list = null;
		Session session = null;
		try {
			session = getSession();
			Query query = session.createQuery(hql);
			// 过滤没参数的情况
			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					query.setString(i, params[i]);
				}
			}
			// 分页
			query.setFirstResult((pageNum - 1) * pageSize);
			query.setMaxResults(pageSize);
			list = query.list();
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
		return list;
	}

	/***
	 * 添加
	 * 
	 * @param obj
	 */
	public static void HQLInsert(Object obj) {
		Session session = null;
		Transaction transaction = null;
		try {
			session = getSession();
			transaction = session.beginTransaction();
			// 保存(插入)
			session.save(obj);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

	/***
	 * 更新(删除)
	 * 
	 * @param hql
	 * @param params
	 */
	public static void HQLUpdate(String hql, String[] params) {
		Session session = null;
		Transaction transaction = null;
		try {
			session = getSession();
			transaction = session.beginTransaction();
			Query query = session.createQuery(hql);
			// 过滤没参数的情况
			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					query.setString(i, params[i]);
				}
			}
			// 更新
			query.executeUpdate();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				transaction.rollback();
			}
			throw new RuntimeException(e.getMessage());
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}
	}

}
